﻿using DelonixRegia.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class SignIn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["acc"] != null) Session["acc"] = null;
        }

        protected void btnSignIn_Click(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(tbxUsername.Text)) && (!string.IsNullOrEmpty(tbxPassword.Text)))
            {
                Account a = AccountManagementDBManager.AuthenticateUser(tbxUsername.Text, tbxPassword.Text);

                if (a != null)
                {
                    Session["acc"] = a;
                    if (string.IsNullOrEmpty(Request.QueryString["ReturnURL"]))
                        Response.Redirect("Default.aspx");
                    else
                        Response.Redirect(HttpUtility.UrlDecode(Request.QueryString["ReturnURL"]));
                }
                else
                {
                    loginBox.Attributes.Remove("style");
                    ClientScriptMgr.showErrorBox("Invalid login credential provided!");
                }
            }
            else
            {
                loginBox.Attributes.Remove("style");
                ClientScriptMgr.showErrorBox("You need to enter your username and password!");
            }
        }
    }
}